package net.caixiaomi.info.ui.dialog;

import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

import com.alibaba.fastjson.JSON;
import com.google.gson.reflect.TypeToken;

import net.caixiaomi.info.R;
import net.caixiaomi.info.adapter.DateAdapter;
import net.caixiaomi.info.base.BaseDialogFragment;
import net.caixiaomi.info.base.quickadapter.BaseQuickAdapter;
import net.caixiaomi.info.model.CommonEvent;
import net.caixiaomi.info.model.CustomerModel;
import net.caixiaomi.info.util.EventWhat;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import butterknife.BindView;

import static android.R.id.list;

/**
 * 账户明细筛选
 */

public class AccountsFilterDialogFragment extends BaseDialogFragment implements BaseQuickAdapter.OnItemClickListener {

    @BindView(list)
    RecyclerView mListView;
    private DateAdapter mAdapter;
    private List<CustomerModel> mData = new ArrayList<>();

    @Override
    protected int layoutRes() {
        return R.layout.dialog_account_filter;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        String strData = getArguments().getString("data");
        List<CustomerModel> data = JSON.parseObject(strData, new TypeToken<List<CustomerModel>>() {}.getType());
        if (data != null && !data.isEmpty()) {
            mData.addAll(data);
        }
    }

    @Override
    protected void onViewCreated(@NonNull View view) {
        Window window = getDialog().getWindow();
        WindowManager.LayoutParams wlp = window.getAttributes();
        wlp.width = ViewGroup.LayoutParams.MATCH_PARENT;
        wlp.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        wlp.gravity = Gravity.BOTTOM;
        window.setAttributes(wlp);
        DisplayMetrics dm = new DisplayMetrics();
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
        window.setBackgroundDrawable(new ColorDrawable(0xff000000));
        getDialog().getWindow().setLayout(dm.widthPixels, getDialog().getWindow().getAttributes().height);

        mListView.setLayoutManager(new LinearLayoutManager(getActivity()));
        mAdapter = new DateAdapter(R.layout.item_date);
        mAdapter.setOnItemClickListener(this);
        mAdapter.setNewData(mData);
        mListView.setAdapter(mAdapter);

        getDialog().setCanceledOnTouchOutside(true);
        getDialog().setCancelable(true);
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        try {
            Iterator<CustomerModel> iterator = mAdapter.getData().iterator();
            while (iterator.hasNext()) {
                CustomerModel next = iterator.next();
                next.setSelected(false);
            }
            mAdapter.getData().get(position).setSelected(true);
            mAdapter.notifyDataSetChanged();
            String text = JSON.toJSONString(mAdapter.getData());
            EventBus.getDefault().post(new CommonEvent(EventWhat.EVENT_ACCOUNT_REFRESH,text));
            dismissAllowingStateLoss();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
